package com.ruoyi.stock2.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class StringExample2 {
	//做饭
    static void doCook(Pot pot, Vegetables vt) {
    	if(pot != null) {
    		System.out.println("锅是好锅，没问题");
    		if(vt != null) {
        		System.out.println("蔬菜很新鲜，没问题");
        	}
    	}else {
    		System.out.println("锅还没到，做个屁");
    	}
    }
    //锅
    static class Pot {}
    //蔬菜
    static class Vegetables {}

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long startTime = System.currentTimeMillis();
        
        /**
         * 1网购买锅
         */
        Callable<Pot> getPot = new Callable<Pot>() {
            @Override
            public Pot call() throws Exception {
            	System.out.println("买锅：执行下单");
                System.out.println("买锅：等待送货");
                TimeUnit.SECONDS.sleep(10);  // 模拟送货时间：延迟2秒
                System.out.println("买锅：快递送到");
                return new Pot();
            }
            
        };
        FutureTask<Pot> task = new FutureTask<Pot>(getPot);
        new Thread(task).start();
        /**
         * 2去超市买菜
         */
        TimeUnit.SECONDS.sleep(2);  // 模拟买菜时间：延迟1秒
        Vegetables vt = new Vegetables();
        System.out.println("买菜：菜已到位");
        /**
         * 做饭
         */
        if (!task.isDone()) {  // 询问快递员，是否到货
            System.out.println("做饭：锅还没到（根据自己的需要，可以调用cancel方法，直接取消订单）");
        }
        Pot pot = task.get();
        System.out.println("做饭：是时候展现真正的技术啦");
        doCook(pot, vt);
        System.out.println("总共用时" + (System.currentTimeMillis() - startTime) + "ms");
    }

}
